<?
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Web Solutions
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2006/03/06 20:23:49 $)
// $RCSfile: functions_misc.php,v $ : $Revision: 1.6 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================


if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

/**
* Render the Parser Ban Form
*/
function renderParserBanForm($type)
{
	global $_SWIFT;

	printFormStart();
	printMainTableHeader(iif($type==INSERT,$_SWIFT["language"]["insertnewban"],$_SWIFT["language"]["editban"]));

	printTextRow("email", $_SWIFT["language"]["bannedemail"], $_SWIFT["language"]["desc_bannedemail"], "text", $_POST["email"]);

	printSubmitRow(iif($type==INSERT,$_SWIFT["language"]["insert"], $_SWIFT["language"]["update"]));

	printMainTableFooter();
	addHiddenField("_m", "parser");
	addHiddenField("_a", iif($type==INSERT, "emailbans", "editemailban"));
	if ($type == INSERT)
	{
		addHiddenField("insertban", "1");
	} else {
		addHiddenField("step", "1");
		addHiddenField("parserbanid", $_POST["parserbanid"]);
	}
	printFormEnd();
}

/**
* Render the Catch-All Rules Form
*/
function renderCatchAllRuleForm($type)
{
	global $dbCore, $_SWIFT;

	printFormStart();
	printMainTableHeader(iif($type==INSERT,$_SWIFT["language"]["insertrule"],$_SWIFT["language"]["editrule"]));

	printTextRow("title", $_SWIFT["language"]["ruletitle"], $_SWIFT["language"]["desc_ruletitle"], "text", $_POST["title"]);

	printTextareaRow("ruleexpr", $_SWIFT["language"]["rregexp"], $_SWIFT["language"]["desc_rregexp"], $_POST["ruleexpr"], "40", "5");

	if ($type == INSERT)
	{
		$_POST["sortorder"] = count($_SWIFT["parsercatchallcache"])+1;
	}
	printTextRow("sortorder", $_SWIFT["language"]["sortorder"], $_SWIFT["language"]["desc_sortorder"], "text", $_POST["sortorder"], "5");

	$_options = array();
	$index = 0;
	$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."emailqueues` ORDER BY `email` ASC;", 4);
	while ($dbCore->nextRecord(4))
	{
		if ($type == EDIT && $_POST["emailqueueid"] == $dbCore->Record4["emailqueueid"])
		{
			$_options[$index]["selected"] = true;
		}
		$_options[$index]["title"] = $dbCore->Record4["email"];
		$_options[$index]["value"] = $dbCore->Record4["emailqueueid"];
		$index++;
	}
	if (count($_options) == 0)
	{
		$_options[$index]["title"] = $_SWIFT["language"]["noemailqueueadd"];
		$_options[$index]["value"] = "";
		$errormessage = $_SWIFT["language"]["errnoqueue"];
	}
	printSelectRow("emailqueueid", $_SWIFT["language"]["emailqueue"], $_SWIFT["language"]["desc_emailqueue"], $_options);

	printSubmitRow(iif($type==INSERT,$_SWIFT["language"]["insert"], $_SWIFT["language"]["update"]));

	printMainTableFooter();
	addHiddenField("_m", "parser");
	addHiddenField("_a", iif($type==INSERT, "catchallrules", "editcatchallrule"));
	if ($type == INSERT)
	{
		addHiddenField("insertrule", "1");
	} else {
		addHiddenField("step", "1");
		addHiddenField("catchallruleid", $_POST["catchallruleid"]);
	}
	printFormEnd();
}

/**
* Deletes the given Parser Bans
*/
function deleteParserBans($parserbanidlist)
{
	global $dbCore;

	$dbCore->query("DELETE FROM `". TABLE_PREFIX ."parserbans` WHERE `parserbanid` IN (". buildIN($parserbanidlist) .");");

	rebuildParserBansCache();

	return true;
}

/**
* Updates the Parser Ban
*/
function updateParserBan($parserbanid, $email)
{
	global $dbCore, $_SWIFT;

	// See if there is a similar email?
	$_parserbans = $dbCore->queryFetch("SELECT * FROM `". TABLE_PREFIX ."parserbans` WHERE `email` = '". $dbCore->escape($email) ."';");
	if (!empty($_parserbans["email"]))
	{
		return false;
	}

	$dbCore->query("UPDATE `". TABLE_PREFIX ."parserbans` SET `email` = '". $dbCore->escape($email) ."', `staffid` = '". intval($_SWIFT["staff"]["staffid"]) ."', `dateline` = '". DATENOW ."' WHERE `parserbanid` = '". intval($parserbanid) ."';", 1, true);

	rebuildParserBansCache();

	return true;
}

/**
* Adds a new Parser ban
*/
function insertParserBan($emaillist)
{
	global $dbCore, $_SWIFT;

	if (!_is_array($emaillist))
	{
		return false;
	}

	$_existingemaillist = array();
	$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."parserbans` WHERE `email` IN (". buildIN($emaillist) .");");
	while ($dbCore->nextRecord())
	{
		$_existingemaillist[] = $dbCore->Record["email"];
	}

	$_sql = array();
	foreach ($emaillist as $key=>$val)
	{
		if (!in_array($val, $_existingemaillist))
		{
			$_sql[] = "('". $dbCore->escape($val) ."', '". DATENOW ."', '". intval($_SWIFT["staff"]["staffid"]) ."')";
		}
	}

	if (count($_sql))
	{
		$dbCore->query("INSERT INTO `". TABLE_PREFIX ."parserbans` (`email`, `dateline`, `staffid`) VALUES ". implode(",", $_sql) .";");
	}

	rebuildParserBansCache();

	return true;
}

/**
* Deletes the given Catch-All rules
*/
function deleteCatchAllRules($catchallruleidlist)
{
	global $dbCore;

	$dbCore->query("DELETE FROM `". TABLE_PREFIX ."catchallrules` WHERE `catchallruleid` IN (". buildIN($catchallruleidlist) .");");

	rebuildParserCatchAllCache();

	return true;
}

/**
* Updates the given Catch-All Rule
*/
function updateCatchAllRule($catchallruleid, $title, $ruleexpr, $emailqueueid, $sortorder)
{
	global $dbCore;

	$dbCore->query("UPDATE `". TABLE_PREFIX ."catchallrules` SET `title` = '". $dbCore->escape($title) ."', `ruleexpr` = '". $dbCore->escape($ruleexpr) ."', `emailqueueid` = '". intval($emailqueueid) ."', `sortorder` = '". intval($sortorder) ."' WHERE `catchallruleid` = '". intval($catchallruleid) ."';");

	rebuildParserCatchAllCache();

	return true;
}

/**
* Inserts a new Catch All Rule
*/
function insertCatchAllRule($title, $ruleexpr, $emailqueueid, $sortorder)
{
	global $dbCore;

	$dbCore->query("INSERT INTO `". TABLE_PREFIX ."catchallrules` (`title`, `ruleexpr`, `emailqueueid`, `dateline`, `sortorder`) VALUES ('". $dbCore->escape($title) ."', '". $dbCore->escape($ruleexpr) ."', '". intval($emailqueueid) ."', '". DATENOW ."', '". intval($sortorder) ."');");

	rebuildParserCatchAllCache();
}

/**
* Rebuilds the Parser Bans Cache
*/
function rebuildParserBansCache()
{
	global $dbCore, $datastore;

	$banscache = array();

	$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."parserbans`;");
	while ($dbCore->nextRecord())
	{
		$banscache[] = $dbCore->Record["email"];
	}

	$datastore->update("parserbanscache", $banscache);
}

/**
* Rebuilds the Catch-All Rules Cache
*/
function rebuildParserCatchAllCache()
{
	global $dbCore, $datastore;

	$_cache = array();

	$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."catchallrules` ORDER BY `sortorder` ASC;");
	while ($dbCore->nextRecord())
	{
		$_cache[$dbCore->Record["catchallruleid"]] = $dbCore->Record;
	}

	$datastore->update("parsercatchallcache", $_cache);
}
?>